package com.woniuxy.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.sales.mapper.*;
import com.woniuxy.sales.model.entity.Admins;
import com.woniuxy.sales.model.entity.Appointments;
import com.woniuxy.sales.model.entity.Reviews;
import com.woniuxy.sales.model.entity.Users;
import com.woniuxy.sales.model.vo.ProductsVo;
import com.woniuxy.sales.service.AdminsService;
import com.woniuxy.sales.untils.JwtTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 *【管理员表】
*/
@Service
public class AdminsServiceImpl extends ServiceImpl<AdminsMapper, Admins>
    implements AdminsService {
    @Autowired
    private AdminsMapper adminsMapper;
    @Autowired
    private JwtTemplate jwtTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private AppointmentsMapper appointmentsMapper;
    @Autowired
    private ReviewsMapper reviewsMapper;

    @Override
    public ResponseEntity<?> login(Admins admins) {
        LambdaQueryWrapper<Admins> eq = Wrappers.lambdaQuery(Admins.class).eq(Admins::getUsername, admins.getUsername());
        Admins admins1 = adminsMapper.selectOne(eq);
        if (admins1 == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户不存在");
        }
        if (admins.getPassword().equals(admins1.getPassword())) {
            String token = createToken(admins1);
            return ResponseEntity.ok(token);
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密码错误");
    }

    public String createToken(Admins admins) {
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("adminsId", admins.getAdminId());
        claims.put("userName", admins.getUsername());
        String token = jwtTemplate.createToken(claims);
        String key = "admins:token:" + admins.getAdminId();
        redisTemplate.opsForValue().set(key, token);
        return token;
    }

    public Admins getAdminsByUsername(Admins admins) {
        LambdaQueryWrapper<Admins> eq = Wrappers.lambdaQuery(Admins.class).eq(Admins::getUsername, admins.getUsername());
        Admins admins1 = adminsMapper.selectOne(eq);
        return admins1;
    }


    @Override
    public Page<Users> findUsers(Integer pageNum, Integer pageSize, String username, String phone) {
        Page<Users> usersPage = new Page<>(pageNum,pageSize);
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            usersQueryWrapper.like("username", username);
        }
        if (phone != null && !phone.isEmpty()) {
            usersQueryWrapper.like("phone", phone);
        }
        return usersMapper.selectPage(usersPage, usersQueryWrapper);

    }

    @Override
    public Page<ProductsVo> selectAllProducts(Integer pageNum, Integer pageSize, String categoryName,String name, String energy,int seatCount) {
        Page<ProductsVo> productsVoPage = new Page<>(pageNum,pageSize);
        return productsMapper.selectAll(productsVoPage,categoryName,name,energy,seatCount);
    }

    @Override
    public Page<Appointments> selectAllAppointments(Integer pageNum, Integer pageSize,String telephone) {
        Page<Appointments> appointmentsPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Appointments> wrapper = Wrappers.lambdaQuery(Appointments.class).orderByAsc(Appointments::getStatus);
        if (telephone != null && !telephone.isEmpty()) {
            wrapper.like(Appointments::getTelephone, telephone);
        }
        return appointmentsMapper.selectPage(appointmentsPage, wrapper);
    }

    @Override
    public Page<Reviews> selectAllReviews(Integer pageNum, Integer pageSize) {
       Page<Reviews> reviewsPage = new Page<>(pageNum,pageSize);
       LambdaQueryWrapper<Reviews> wrapper = Wrappers.lambdaQuery(Reviews.class).orderByDesc(Reviews::getReviewDate);
       return reviewsMapper.selectPage(reviewsPage, wrapper);
    }

    @Override
    public void updatePrice(int productId, int newPrice) {
        if (productId <= 0 || newPrice < 0) {
            throw new IllegalArgumentException("无效的产品ID或价格");
        }
        productsMapper.updatePrice(productId, newPrice);
    }

    @Override
    public void updateStock(int productId, int newStock) {
        if (productId <= 0 || newStock < 0) {
            throw new IllegalArgumentException("无效的产品ID或库存");
        }
        productsMapper.updateStock(productId, newStock);
    }



}







